    /**
     * Copyright 2014 戴文龙 (vistier515@gmail.com). All rights reserved.
     * File Name: android.zhibo8.request.common.JsonResponseHandler.java
     * Encoding UTF-8
     * Version: 1.0
     * Date: 2014-7-3
     */

    package com.hequ.merchant.banner.handler;

    import android.text.TextUtils;

    import com.hequ.merchant.banner.entity.Page;

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.List;

    /**
     * @author 戴文龙 (vistier515@gmail.com)
     * @version Revision: 1.0.0
     * @Date 2014-7-3
     * @History
     */
    public abstract class JsonResponseHandler<T extends Serializable> {

        private static final String JSON_KEY_STATUS = "status";
        private static final String JSON_KEY_RESULT = "result";
        private static final String JSON_KEY_RESULTS = "results";
        private static final String JSON_KEY_EXCEPTION = "exception";
        private static final String JSON_KEY_PAGING = "paging";
        private static final String JSON_KEY_TIMESTAMP = "timestamp";

        protected  static final String JSON_KEY_ID = "id";

        private Page<T> page = new Page<T>();

        private boolean isSuccess = false;

        protected JsonResponseHandler() {
        }

        public JsonResponseHandler<T> parse(String json) throws JSONException {
            return parse(new JSONObject(json));
        }

        public JsonResponseHandler<T> parse(JSONObject json) throws JSONException {

            if (hasKeyValue(json, JSON_KEY_STATUS)) {
                page.setStatus(json.getInt(JSON_KEY_STATUS));
            }

            if (hasKeyValue(json, JSON_KEY_RESULT)) {
                page.setResult(parseItem(json.getJSONObject(JSON_KEY_RESULT)));
            }

            if (hasKeyValue(json, JSON_KEY_RESULTS)) {
                page.setResults(parseItems(json.getJSONArray(JSON_KEY_RESULTS)));
            }

            if(hasKeyValue(json, JSON_KEY_EXCEPTION)) {
                page.setException(json.getString(JSON_KEY_EXCEPTION));
            }

            if(hasKeyValue(json, JSON_KEY_PAGING)) {
                page.setPaging(new PageJsonResponseHandler().parseItem(json.getJSONObject(JSON_KEY_PAGING)));
            }

            if(hasKeyValue(json, JSON_KEY_TIMESTAMP)) {
                page.setTimestamp(json.getLong(JSON_KEY_TIMESTAMP));
            }
            return this;
        }

        protected List<T> parseItems(JSONArray json) throws JSONException {
            List<T> data = new ArrayList<>();

            for (int i = 0; i < json.length(); i++) {
                data.add(parseItem(json.getJSONObject(i)));
            }

            return data;
        }


        protected abstract T parseItem(JSONObject json) throws JSONException;

        public Page<T> getParsedPage() {
            return page;
        }

        public List<T> getParsedItems() {
            return page.getResults();
        }

        public T getParsedItem() {
            return page.getResult();
        }

        public boolean isSuccess() {
            return isSuccess;
        }

        public static boolean hasKeyValue(JSONObject json, String key) throws JSONException {
            return !json.isNull(key) && json.has(key) && !TextUtils.isEmpty(json.getString(key)) && !"null NULL".contains(json.getString(key));
        }

        public void clean() {
            page = new Page<>();
        }


    }
